package com.cnswhy.cloud.server.system.service.impl;


import cn.hutool.core.convert.Convert;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.system.dao.SystemRoleAuthorityMapper;
import com.cnswhy.cloud.server.system.dto.RoleAuthoritySaveDTO;
import com.cnswhy.cloud.server.system.entity.SystemRoleAuthority;
import com.cnswhy.cloud.server.system.enumeration.AuthorizeType;
import com.cnswhy.cloud.server.system.service.SystemResourceService;
import com.cnswhy.cloud.server.system.service.SystemRoleAuthorityService;
import com.cnswhy.cloud.common.core.base.service.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 角色的资源
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-13
 */
@Slf4j
@Service

public class SystemRoleAuthorityServiceImpl extends SuperServiceImpl<SystemRoleAuthorityMapper, SystemRoleAuthority> implements SystemRoleAuthorityService {
    @Autowired
    SystemResourceService systemResourceService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRoleAuthority(RoleAuthoritySaveDTO dto) {
        //删除角色和资源的关联
        super.remove(Wraps.<SystemRoleAuthority>lbQ().eq(SystemRoleAuthority::getRoleId, dto.getRoleId()));

        List<SystemRoleAuthority> list = new ArrayList<>();
        if (dto.getResourceIdList() != null && !dto.getResourceIdList().isEmpty()) {
            List<Long> menuIdList = systemResourceService.findMenuIdByResourceId(dto.getResourceIdList());
            if (dto.getMenuIdList() == null || dto.getMenuIdList().isEmpty()) {
                dto.setMenuIdList(menuIdList);
            } else {
                dto.getMenuIdList().addAll(menuIdList);
            }

            //保存授予的资源
            List<SystemRoleAuthority> resourceList = new HashSet<>(dto.getResourceIdList())
                    .stream()
                    .map((resourceId) -> SystemRoleAuthority.builder()
                            .authorityType(AuthorizeType.RESOURCE)
                            .authorityId(resourceId)
                            .roleId(dto.getRoleId())
                            .build())
                    .collect(Collectors.toList());
            list.addAll(resourceList);
        }
        if (dto.getMenuIdList() != null && !dto.getMenuIdList().isEmpty()) {
            //保存授予的菜单
            List<SystemRoleAuthority> menuList = new HashSet<>(dto.getMenuIdList())
                    .stream()
                    .map((menuId) -> SystemRoleAuthority.builder()
                            .authorityType(AuthorizeType.MENU)
                            .authorityId(menuId)
                            .roleId(dto.getRoleId())
                            .build())
                    .collect(Collectors.toList());
            list.addAll(menuList);
        }
        super.saveBatch(list);

        // 清理
        //List<Long> userIdList = userRoleService.listObjs(Wraps.<UserRole>lbQ().select(UserRole::getUserId).eq(UserRole::getRoleId, dto.getRoleId()),
        //(userId) -> Convert.toLong(userId, 0L));

        return true;
    }
}
